package com.gq.tab.scr;

import java.util.ArrayList;
import java.util.Hashtable;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.gq.dragonball.ISortInterface;
import com.gq.dragonball.ImageDetailActivity;
import com.gq.dragonball.R;
import com.gq.utils.Client;
import com.rtst.widget.actionbar.ISearchInterface;
import com.rtst.widget.tab.TabFragment;

public class Bo2Scr extends TabFragment implements ISearchInterface, ISortInterface {

	private ListView listView;
	private ViewGroup mLayout;
	private AdView adView;
	private ChapterAdapter adapter;
	private ProgressDialog mProgressDialog;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (mLayout == null) {
			mLayout = (ViewGroup) inflater.inflate(R.layout.bo2_scr, container, false);
			listView = (ListView) mLayout.findViewById(android.R.id.list);
			adView = (AdView) mLayout.findViewById(R.id.adView);
			adapter = new ChapterAdapter(getActivity());
			adapter.load();
			listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {
					Hashtable<String, Object> item = (Hashtable<String, Object>) parent.getItemAtPosition(position);
					Intent intent = new Intent(getActivity(), ImageDetailActivity.class);
					ArrayList<String> images = (ArrayList<String>) item.get("images");
					intent.putExtra("IMAGES", images);
					intent.putExtra("CHAPTER", position + 1);
					startActivity(intent);
				}
			});
		} else {
			((ViewGroup) mLayout.getParent()).removeView(mLayout);
		}
		return mLayout;
	}

	@Override
	public void onResume() {
		super.onResume();
		adView.loadAd(new AdRequest());
	}
	
	private class ChapterAdapter extends BaseAdapter {

		private LayoutInflater mInflater;
    	private ArrayList<Hashtable<String, Object>> data = new ArrayList<Hashtable<String,Object>>();
		
    	public ChapterAdapter(Context c) {
    		mInflater = LayoutInflater.from(c);
    	}
    	
    	public void reverse() {
    		ArrayList<Hashtable<String, Object>> rdata = new ArrayList<Hashtable<String,Object>>();
    		for(int i=data.size()-1;i>=0;i--) {
    			rdata.add(data.get(i));
    		}
    		data = rdata;
    	}
    	
    	public void load() {
//    		mProgressDialog = ProgressDialog.show(getActivity(), "", "Loading ...");
    		Client client = new Client();
            client.excuteAndListen("http://api.vietcntt.net/comic/olongvien?v=2", new Client.IClientListener() {
    			
    			@Override
    			public void onDone(boolean ok, Object ret) {
    				System.out.println(ret.toString());
    				if (mProgressDialog != null && (mProgressDialog.isShowing())) {
    					mProgressDialog.dismiss();
    				}
    				System.out.println(String.valueOf(ret instanceof JSONArray));
    				if (ret instanceof JSONArray) {
    					if (ret != null && ret instanceof JSONArray) {
    						listView.setVisibility(View.VISIBLE);
    						JSONArray ob = (JSONArray) ret;
    						for(int i=0;i<ob.length();i++) {
    							try {
									JSONObject chapterOb = ob.getJSONObject(i);
									Hashtable<String, Object> iitem = new Hashtable<String, Object>();
									iitem.put("chapter", chapterOb.getString("chapter"));
									ArrayList<String> lImages = new ArrayList<String>();
									for(int ii=0;ii<chapterOb.getJSONArray("images").length();ii++) {
										lImages.add(chapterOb.getJSONArray("images").getString(ii));
									}
									iitem.put("images", lImages);
									data.add(iitem);
								} catch (JSONException e) {
									e.printStackTrace();
								}
    						}
    						listView.setAdapter(adapter);
    					}
    				}
    			}
    		});
    	}
    	
		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null) {
				convertView = mInflater.inflate(R.layout.list_item, null, false);
			}
			@SuppressWarnings("unchecked")
			Hashtable<String, Object> item = (Hashtable<String, Object>) getItem(position);
			TextView chapterName = (TextView) convertView.findViewById(R.id.chapter);
			chapterName.setText("Chương " + item.get("chapter").toString());
			return convertView;
		}
	}

	@Override
	public void onSearchComplete(CharSequence in) {
	}

	@Override
	public void onDestroy() {
		if (adView != null) adView.destroy();
		super.onDestroy();
	}
	
	@Override
	public void reverse() {
		if (adapter != null) {
			adapter.reverse();
			adapter.notifyDataSetChanged();
		}
	}

}
